package com.by.yz.welcome;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import com.by.yz.App;
import com.by.yz.R;
import com.by.yz.activity.LoginActivity;
import com.by.yz.custom.CircleImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Abe on 2016/10/6.
 */
public class WelcomeActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
    private View view1, view2, view3;
    private ViewPager viewPager;  //对应的viewPager

    private List<View> viewList;//view数组

    /**将小圆点的图片用数组表示*/
    private CircleImageView[] imageViews;
    private CircleImageView imageView;
    private ViewGroup viewPoints;

    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        LayoutInflater inflater=getLayoutInflater();
        view1 = inflater.inflate(R.layout.guide_first, null);
        view2 = inflater.inflate(R.layout.guide_second,null);
        view3 = inflater.inflate(R.layout.guide_third, null);

        viewPoints = (ViewGroup)findViewById(R.id.viewGroup);
        imageViews=new CircleImageView[3];
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(20,20);
        lp.setMargins(0, 0, 20, 0);
        for(int i=0;i<3;i++){
            imageView = new CircleImageView(WelcomeActivity.this);
            //设置小圆点imageview的参数
            imageView.setLayoutParams(new ViewGroup.LayoutParams(20,20));//创建一个宽高均为20 的布局
            imageView.setPadding(20, 0, 20, 0);
            imageView.setLayoutParams(lp);
            //将小圆点layout添加到数组中
            imageViews[i] = imageView;

            //默认选中的是第一张图片，此时第一个小圆点是选中状态，其他不是
            if(i==0){
                imageViews[i].setBackgroundResource(R.mipmap.page_indicator_focused);
            }else{
                imageViews[i].setBackgroundResource(R.mipmap.page_indicator);
            }

            //将imageviews添加到小圆点视图组
            viewPoints.addView(imageViews[i]);
        }

        viewList = new ArrayList<View>();// 将要分页显示的View装入数组中
        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);

        button=(Button) view3.findViewById(R.id.start);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                App.cacheToken(WelcomeActivity.this);
                startActivity(new Intent(WelcomeActivity.this,LoginActivity.class));
                WelcomeActivity.this.finish();
            }
        });

        PagerAdapter pagerAdapter = new PagerAdapter() {

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                // TODO Auto-generated method stub
                return arg0 == arg1;
            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return viewList.size();
            }

            @Override
            public void destroyItem(ViewGroup container, int position,
                                    Object object) {
                // TODO Auto-generated method stub
                container.removeView(viewList.get(position));
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                // TODO Auto-generated method stub
                container.addView(viewList.get(position));


                return viewList.get(position);
            }
        };
        viewPager.setOnPageChangeListener(this);
        viewPager.setAdapter(pagerAdapter);

    }

    /**
     * 滑动状态改变时调用
     */
    @Override
    public void onPageScrollStateChanged(int arg0) {

    }

    /**
     * 当前页面滑动时调用
     */
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    /**
     * 新的页面被选中时调用
     */
    @Override
    public void onPageSelected(int arg0) {
        for(int i=0;i<imageViews.length;i++){
            imageViews[arg0].setBackgroundResource(R.mipmap.page_indicator_focused);
            //不是当前选中的page，其小圆点设置为未选中的状态
            if(arg0 !=i){
                imageViews[i].setBackgroundResource(R.mipmap.page_indicator);
            }
        }
    }

//    private static final int GO_LOGIN = 100;
//    private static final int GO_GUIDE = 200;
//    NetworkImageView welcomeImg;
//    RequestQueue requestQueue;
//    ImageLoader imageLoader;
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_welcome);
//        //布局全屏显示
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        requestQueue = Volley.newRequestQueue(WelcomeActivity.this);// 网络资源请求队列
//        imageLoader = new ImageLoader(requestQueue, new BitmapCache());// 图片加载类
//        welcomeImg = (NetworkImageView) findViewById(R.id.welcomeImg);
//
//        welcomeImg.setDefaultImageResId(R.mipmap.wel);
//        welcomeImg.setErrorImageResId(R.mipmap.wel);
//        welcomeImg.setImageUrl("22145",
//                imageLoader);
//    }
//
//    @Override
//    protected void onResume() {
//        super.onResume();
//        //判断是否第一次使用App
//        if (App.getCacheToken(WelcomeActivity.this) != null) {
//            //不是第一次使用
//            mHandler.sendEmptyMessageDelayed(GO_LOGIN, 5000);
//        } else {
//            //第一次使用
//            mHandler.sendEmptyMessageDelayed(GO_GUIDE, 5000);
//        }
//    }
//
//    private Handler mHandler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            Intent intent=new Intent();
//            switch (msg.what) {
//                case GO_LOGIN:
////                    if (App.isLoginFlag()) {
////                        //跳转MainActivity
////                    }else {
////                        //跳转登陆界面
////                 }
//                    intent.setClass(WelcomeActivity.this, LoginActivity.class);
//                    startActivity(intent);
//                    finish();
//                    break;
//                case GO_GUIDE:
//                    //跳转GuideActivity
//                    intent.setClass(WelcomeActivity.this, LoginActivity.class);
//                    startActivity(intent);
//                    finish();
//                    break;
//            }
//        }
//
//    };


}
